﻿using System.IO;
using UnityEngine;

namespace com.yd.sdk
{
    public class AdSetting : ISetting
    {
        //fyber,mtg,unity,pangle,applovin,facebook
        private bool _isUseAdmob, _isUseFacebook;

        private bool _isMediation_Facebook,
            _isMediation_fyber,
            _isMediation_mtg,
            _isMediation_unity,
            _isMediation_pangle,
            _isMediation_applovin;

        public void Init()
        {
            _isUseAdmob = SDKEditorWindow.sdkSettings.isOpen_Admob;
            _isMediation_Facebook = SDKEditorWindow.sdkSettings.isOpen_Admob_Facebook;
            _isMediation_unity = SDKEditorWindow.sdkSettings.isOpen_Admob_Unity;
            _isMediation_pangle = SDKEditorWindow.sdkSettings.isOpen_Admob_Pangle;
            _isMediation_mtg = SDKEditorWindow.sdkSettings.isOpen_Admob_MTG;
        }

        public void OnGUI()
        {
            GUILayout.Label("广告SDK模块");
            GUILayout.BeginHorizontal();
            _isUseAdmob = GUILayout.Toggle(_isUseAdmob, "开启Admob");

            // GUILayout.Space(0);
            // _isUseFacebook = GUILayout.Toggle(_isUseFacebook, "开启Facebook");
            // if (_isUseFacebook)
            // {
            //     _isUseAdmob = false;
            // }

            GUILayout.EndHorizontal();
            
            if (_isUseAdmob)
            {
                GUILayout.BeginHorizontal();

                _isMediation_Facebook = GUILayout.Toggle(_isMediation_Facebook, "开启Mediation_Facebook");
                _isMediation_unity = GUILayout.Toggle(_isMediation_unity, "开启Mediation_unity");
                _isMediation_pangle = GUILayout.Toggle(_isMediation_pangle, "开启Mediation_pangle");
                _isMediation_mtg = GUILayout.Toggle(_isMediation_mtg, "开启Mediation_mtg");
                
                _isMediation_fyber = GUILayout.Toggle(_isMediation_fyber, "开启Mediation_fyber(预留)");
                _isMediation_applovin = GUILayout.Toggle(_isMediation_applovin, "开启Mediation_applovin(预留)");
                
                GUILayout.EndHorizontal();
                // _isUseFacebook = false;
            }
            
        }

        public void Apply()
        {
            if (_isUseAdmob)
            {
                //安装
                InstallAdmob();
                if (_isMediation_Facebook)
                    InstallAdmob_Mediation_Facebook();
                else
                    RemoveAdmob_Mediation_Facebook();

                if (_isMediation_unity)
                    InstallAdmob_Mediation_Unity();
                else
                    RemoveAdmob_Mediation_Unity();

                if (_isMediation_pangle)
                    InstallAdmob_Mediation_Pangle();
                else
                    RemoveAdmob_Mediation_Pangle();

                if (_isMediation_mtg)
                    InstallAdmob_Mediation_mtg();
                else
                    RemoveAdmob_Mediation_mtg();

                

            }
            else
            {
                //移除
                RemoveAdmob();
                RemoveAdmob_Mediation_Facebook();
                RemoveAdmob_Mediation_Unity();
                RemoveAdmob_Mediation_Pangle();
                RemoveAdmob_Mediation_mtg();
            }

            SDKEditorWindow.sdkSettings.isOpen_Admob = _isUseAdmob;
            SDKEditorWindow.sdkSettings.isOpen_Admob_Facebook = _isMediation_Facebook;
            SDKEditorWindow.sdkSettings.isOpen_Admob_Unity = _isMediation_unity;
            SDKEditorWindow.sdkSettings.isOpen_Admob_Pangle = _isMediation_pangle;
            SDKEditorWindow.sdkSettings.isOpen_Admob_MTG = _isMediation_mtg;
        }

      


        public bool IsSelectedSomething()
        {
            return _isUseAdmob;
        }

        #region Mediation_Facebook

        private void InstallAdmob_Mediation_Facebook()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADMOB_MEDIATION_FACEBOOK_DEPENDENCIES);
            //ADMOB_MEDIATION_FACEBOOK_XML
            Utils.CopyAndroidXml(Constants.GetFullPath(Constants.ADMOB_MEDIATION_FACEBOOK_XML, true),
                Constants.GetFullPath(Constants.ANDROID_PLUGINS_RES_STRING_PATH + "network_security_config.xml"));
            
            //还需要给AndroidManifest.xml中的Application中加入

            string str = "android:networkSecurityConfig=\"@xml/network_security_config\"";
            Utils.AddApplicationProperty("networkSecurityConfig","@xml/network_security_config");
        }

        private void RemoveAdmob_Mediation_Facebook()
        {
            var destPath =
                Constants.GetFullPath(Constants.ANDROID_PLUGINS_RES_STRING_PATH + "network_security_config.xml");
            if (File.Exists(destPath))
                File.Delete(destPath);

            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADMOB_MEDIATION_FACEBOOK_DEPENDENCIES);
            
        }

        #endregion

        #region Mediation_Unity
        private void InstallAdmob_Mediation_Unity()
        {
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADMOB_MEDIATION_UNITY_DEPENDENCIES);
            
        }

        private void RemoveAdmob_Mediation_Unity()
        {
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH), 
                Constants.ADMOB_MEDIATION_UNITY_DEPENDENCIES);

        }
        #endregion

        #region Mediation_Pangle
        
        private void InstallAdmob_Mediation_Pangle()
        {
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADMOB_MEDIATION_PANGLE_DEPENDENCIES);
            
            //Utils.AddApplicationProperty("networkSecurityConfig","@xml/network_security_config");
            Utils.AddApplicationMetaData("com.yd.sdk.pangleID",SDKEditorWindow.sdkSettings.Pangle_AppId);
        }
        
        private void RemoveAdmob_Mediation_Pangle()
        {
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH), 
                Constants.ADMOB_MEDIATION_PANGLE_DEPENDENCIES);
            Utils.RemoveApplicationMetaData("com.yd.sdk.pangleID");
        }
        
        #endregion

        #region Mediation_MTG

        private void InstallAdmob_Mediation_mtg()
        {
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADMOB_MEDIATION_MTG_DEPENDENCIES);
            
            // Utils.CopyAndroidXml(Constants.GetFullPath(Constants.ADMOB_MEDIATION_MTG_ADAPT, true),
            //     Constants.GetFullPath(Constants.ANDROID_PLUGINS_PATH + "admob_adapter-3.0.0.aar"));
            
        }

        
        private void RemoveAdmob_Mediation_mtg()
        {
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH), 
                Constants.ADMOB_MEDIATION_MTG_DEPENDENCIES);
            
            var destPath =
                Constants.GetFullPath(Constants.ANDROID_PLUGINS_PATH + "admob_adapter-3.0.0.aar");
            if (File.Exists(destPath))
                File.Delete(destPath);
        }

       

        #endregion
        
        
        #region Admob

        private void InstallAdmob()
        {
            //向Gradle中添加依赖库
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADMOB_DEPENDENCIES);
            
            //添加权限
            Utils.MergeAndroidManifest(Constants.GetFullPath(Constants.ADMOB_MANIFEST, true),
                Constants.GetFullPath(Constants.ANDROID_MAIN_MANIFEST));
            
            //添加实现包
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ADMOB_IMP_DEPENDENCIES);
            
        }

        private void RemoveAdmob()
        {
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_MANIFEST, true),
                Constants.ANDROID_MAIN_MANIFEST);
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t" + Constants.ADMOB_DEPENDENCIES + "\r");
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                "\t" + Constants.ADMOB_IMP_DEPENDENCIES + "\r");
        }

        #endregion
        
        

        
        
    }
}